package com.example.work1month12begin.work.controller;

import com.example.work1month12begin.work.mapper.CityMapper;
import com.example.work1month12begin.work.pojo.City;
import com.example.work1month12begin.work.service.CityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/city")
@Api(tags = "城市控制器",value = "控制城市信息")
public class CityConteroller {

    @Autowired
    CityMapper cityMapper;

    @Autowired
    CityService cityService;

    @GetMapping("/all")
    @ApiOperation(value = "城市查询",notes = "查询城市所有信息")
    public ModelAndView selectAll(){
        List<City> cities1 = cityMapper.selectAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("citys",cities1);
        return modelAndView;
    }

    @GetMapping("/one/{cityId}")
    @ApiOperation(value = "查询单挑城市",notes = "通过Id查询单挑城市信息")
    @ApiImplicitParam(name = "cityId",value = "城市Id",dataType = "int",example = "1")
    public ModelAndView selctOne(@PathVariable(name = "cityId") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/update.html");
        modelAndView.addObject("city",cityMapper.selectById(id));
        return modelAndView;
    }

    @RequestMapping("/page/{pageNumber}/{number}")
    public ModelAndView selectPage(@PathVariable(name = "pageNumber")Integer pageNumber,@PathVariable(name = "number")Integer number){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("city",cityService.findPage(pageNumber,number));
        return modelAndView;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加城市",notes = "添加一个城市信息")
    @ApiImplicitParam(name = "city",value = "城市实体",dataType = "City")
    public ModelAndView insert(City city){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("inset",cityMapper.insert(city));
        modelAndView.setViewName("redirect:/city/all");
        return modelAndView;
    }

    @GetMapping("/delete/{cityId}")
    @ApiOperation(value = "删除城市",notes = "删除城市信息")
    @ApiImplicitParam(name = "cityId",value = "城市编号",dataType = "int",example = "1")
    public ModelAndView delete(@PathVariable(name = "cityId")Integer id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("delete",cityMapper.deleteById(id));
        modelAndView.setViewName("forward:/city/all");
        return modelAndView;
    }

    @PutMapping("/update/{cityId}")
    @ApiOperation(value = "修改城市新内容",notes = "修改一条城市信息")
    @ApiImplicitParam(name = "city",value = "城市实体",dataType = "City")
    public ModelAndView update(City city){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("chance",cityMapper.updateById(city));
        return modelAndView;
    }
}
